package linwen.wang;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.boot.autoconfigure.cache.CacheProperties;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName TestCaffeine
 * @Description TODO
 * @Author zero
 * @DATE 2023/8/7 9:38 PM
 * @Version 1.0
 */
public class TestCaffeine {
    public static void main(String[] args) throws InterruptedException {
        Cache<String, String> cache = Caffeine.newBuilder()
                //设置过期时间；向缓存中写入的数据会在1分钟之后过期
                .expireAfterWrite(1, TimeUnit.MINUTES)
                // 过期时间，读取缓存后1s后过期
                .expireAfterAccess(1,TimeUnit.SECONDS)
                //设置最大值；最大可以放1条数据,当数据量超过最大值之后，则进行覆盖
                .maximumSize(1)
                .build();

        cache.put("key","value1");
        cache.put("key2","value2");
        cache.put("key2","value222");
        cache.put("key3","value333");
        Thread.sleep(1000);
        System.out.println(cache.getIfPresent("key"));
        System.out.println(cache.get("key3",(key)-> "66666"));

    }
}
